package com.flowerSystem.controller;

import com.flowerSystem.entity.User;
import com.flowerSystem.service.UserService;
import com.flowerSystem.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/manager")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

    //查询员工数据
    @GetMapping("/employeeuser")
    public ResponseResult selectEmployeeUser(){
        List<User> userList = userService.allEmployeeUser();
        return ResponseResult.ok(userList);
        }

    //删除员工
    @DeleteMapping("/employeeuser")
    public ResponseResult deleteEmployeeUser(@RequestBody User user){
        System.out.println(user);
        boolean f =userService.removeById(user);
        userService.removeUserRole(user.getId());
        if (f){
            return ResponseResult.ok(user);
        }
        return ResponseResult.fail();
    }
    //添加员工
    @PostMapping("/employeeuser")
    public ResponseResult upEmployeeUser(@RequestBody User user){
        System.out.println(user);
        if (userService.upUser(user)){
            return ResponseResult.ok(user);
        }
        return ResponseResult.fail();
    }
    //修改员工
    @PutMapping("/employeeuser")
    public ResponseResult updateEmployeeUser(@RequestBody User user){
        System.out.println(user);
        boolean f = userService.updateById(user);
        if (f){
            return ResponseResult.ok(user);
        }
        return ResponseResult.fail();
    }

    //查询用户
    @GetMapping("/user/getUser")
    public ResponseResult selectUser(){
        return ResponseResult.ok(userService.getUser());
    }

//    public ResponseResult


}
